package com.loongtech.bi.action.biflow;

import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.Map.Entry;

/**
 * 
 * ClassName: FightRankingListNewAction
 * 
 * @Description: 战斗力排行
 * @author liuchen
 * @date 2017-5-17 下午5:05:00
 */
@Controller
@Scope("prototype")
@RequestMapping("/flow/fightRankingListFlow")
public class FightRankingListNewAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
	@Autowired
	HadoopQueryManager hadoopQueryManager;

	/**
	 * 
	 * @Description: TODO 获取参数
	 * @param @param request
	 * @param @param response
	 * @param @param callback
	 * @return void
	 * @throws
	 * @author liuchen
	 */
	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
		try {
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "0");
			result.put("serverItmes", serverItmes);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String params,
			String callback) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> m = jsonToMap(params);
			Date dateEnd = Time.formatDate((String) m.get("dateEnd"), "yyyy-MM-dd");

			String serverid = (String) m.get("serverid");

			if (HadoopQueryManager.VISIT_QUEUE.offer("FightRankingListNewAction")) {
				System.out.println("VISIT_QUEUE offer FightRankingListNewAction ok!");

				StringBuilder sql = new StringBuilder();
				sql.append("select b.platform,b.channel,b.serverid,b.userid,b.rolename,b.roleid,a.fightvalue, a.logtime from (select aa.roleid,aa.logtime,aa.dt,aa.fightvalue from fightvaluerank  aa,(select roleid,max(logtime) logtime from fightvaluerank where");
				sql.append(" dt <= '");
				sql.append(Time.getDateStr(dateEnd));
				sql.append("' group by roleid) bb where ");
				sql.append("aa.dt<='");
				sql.append(Time.getDateStr(dateEnd));
				sql.append(" ' and aa.roleid = bb.roleid and aa.logtime = bb.logtime) as a left join (select cc.platform,cc.channel,cc.serverid,cc.userid,cc.rolename,cc.roleid from rolecreate  cc,(select roleid,max(logtime) logtime from rolecreate where ");
				sql.append(" dt <= '");
				sql.append(Time.getDateStr(dateEnd));
				sql.append("' group by roleid) dd where");
				sql.append(" cc.dt <= '");
				sql.append(Time.getDateStr(dateEnd));
				sql.append(" ' and cc.roleid = dd.roleid and cc.logtime = dd.logtime) as b on a.roleid = b.roleid ");
				if (!"0".equals(serverid)) {
					sql.append("where b.serverid = ");
					sql.append(serverid);
				}
				sql.append(" order by a.fightvalue desc limit 100;");
				System.out.println(sql.toString());
				Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
				if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue()
						|| ret.getValue().size() <= 0) {
                    result.put("res", new ArrayList<Object[]>());
                    result.put("data", "");
                    printToJson(response, result, callback);
					return;
				}
				List<Object[]> lo = ret.getValue();

				for (Object[] o : lo) {
					o[0] = BiPlatformManager.getPlatformNameById((String) o[0]);
					o[1] = BiPlatformManager.getChannelNameById((String) o[1]);
					o[2] = BiPlatformManager.getServerNameById((String) o[2]);
				}
				result.put("res", "0");
				result.put("data", lo);
				printToJson(response, result, callback);
			} else {
				System.out.println("VISIT_QUEUE offer FightRankingListNewAction failed!");
				result.put("res", "1");
				result.put("msg", "当前系统正被访问，为保证服务器稳定，请稍后再试!");
				printToJson(response, result, callback);
				return;
			}

		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
		}
	}

	@Override
	public void search() {
		// TODO Auto-generated method stub

	}

	@Override
	protected String csvContent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String csvFileName() {
		// TODO Auto-generated method stub
		return null;
	}
}
